<?php

	$root = substr($_SERVER['SCRIPT_FILENAME'], 0, -13);

	include $root."htdocs/global/functions/config.php";
	loadConfig($root);

	if (!config("slave")) die();

	$data = file_get_contents(config("master_data_address")."?export=".config("master_data_shared_key"));

	if (strpos($data, "###ERROR") === true) {
		/* error */
		echo $data;
	} else {

		$delim1 = "###allowed_macs###\n";
		$delim2 = "###allowed_ports_tcp###\n";
		$delim3 = "###allowed_ports_udp###\n";
		$delim4 = "###public_ips###\n";
		$delim5 = "###users###\n";

		$delimPos1 = strpos($data, $delim1);
		$delimPos2 = strpos($data, $delim2);
		$delimPos3 = strpos($data, $delim3);
		$delimPos4 = strpos($data, $delim4);
		$delimPos5 = strpos($data, $delim5);

		$macs = substr($data, $delimPos1+strlen($delim1), $delimPos2-$delimPos1-strlen($delim1));
		$tcp = substr($data, $delimPos2+strlen($delim2), $delimPos3-$delimPos2-strlen($delim2));
		$udp = substr($data, $delimPos3+strlen($delim3), $delimPos4-$delimPos3-strlen($delim3));
		$ips = substr($data, $delimPos4+strlen($delim4), $delimPos5-$delimPos4-strlen($delim4));
		$users = substr($data, $delimPos5+strlen($delim5));

		$save = file_put_contents($root."data/allowed_macs", $macs);
		if (!$save) { echo "ERROR: save allowed_macs\n"; }

		$save = file_put_contents($root."data/allowed_ports_tcp", $tcp);
		if (!$save) { echo "ERROR: save allowed_ports_tcp\n"; }

		$save = file_put_contents($root."data/allowed_ports_udp", $udp);
		if (!$save) { echo "ERROR: save allowed_ports_udp\n"; }

		$save = file_put_contents($root."data/public_ips", $ips);
		if (!$save) { echo "ERROR: save public_ips\n"; }

		$save = file_put_contents($root."data/users.xml", $users);
		if (!$save) { echo "ERROR: save users.xml\n"; }

	}

?>
